package com.qf.servlet;

import com.qf.entity.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * --- 天道酬勤 ---
 *
 * @author Hulboy
 * @date 2024/6/30
 * @desc --- 代码敲烂 月薪过万 ---
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    /**
     * LoginServlet （接受登录请求）
     * 通过方法参数 HttpServletResponse resp完成响应
     */
private UserService userService = new UserServiceImpl();



    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 请求前设置编码格式
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        // 1接受请求数据

        String username = req.getParameter("username");
        System.out.println("接收到username = " + username);
        String password = req.getParameter("password");
        System.out.println("接收到password = " + password);

        User user = userService.login(username, password);
        System.out.println("接收到service返回的对象user = " + user);
    //     其实是UserDaoImpl,service用Dao层操作数据库CRUD
    //     方法是通过预处理，如果库中有匹配的ID和密码,获得（set所有）匹配对象的所有属性，以对象形式返回（return user;）
    // -------------------------------------------------------------------------------


        PrintWriter writer = resp.getWriter();
        if (user != null) {// 说明登录成功
            writer.write("<html>");
            writer.write("<head>");
            writer.write("<title>登录成功</title>");
            writer.write("</head>");
            writer.write("<body>");
            writer.write("<h1>欢迎 [ "+user.getUsername()+" ]</h1>");
            writer.write("<hr>");
            writer.write("<h3>个人信息如下:</h3>");
            writer.write("<table border='2'>");
            writer.write("<thead>");
            writer.write("<td>编号</td>");
            writer.write("<td>用户名</td>");
            writer.write("<td>密码</td>");
            writer.write("<td>手机号</td>");
            writer.write("<td>注册时间</td>");
            writer.write("<td>余额</td>");
            writer.write("<td>性别</td>");
            writer.write("</thead>");
            writer.write("<tr>");
            writer.write("<td>"+user.getId()+"</td>");
            writer.write("<td>"+user.getUsername()+"</td>");
            writer.write("<td>"+user.getPassword()+"</td>");
            writer.write("<td>"+user.getPhone()+"</td>");
            writer.write("<td>"+user.getCreateTime()+"</td>");
            writer.write("<td>"+user.getMoney()+"</td>");
            writer.write("<td>"+(user.getSex() == 1 ? "男":"女")+"</td>");
            writer.write("</tr>");
            writer.write("</table>");
            writer.write("</body>");
            writer.write("</html>");
        } else {
            writer.write("<html>");
            writer.write("<head>");
            writer.write("<title>登录失败</title>");
            writer.write("</head>");
            writer.write("<body>");
            writer.write("<h1 style='color:red'>用户名或密码错误</h1>");
            writer.write("</body>");
            writer.write("</html>");
        }


    //     在此窗口调用对象的get属性（配合PrintWriter）列出匹配的整行数据
    //     这也叫做出响应


    }
}
